<?php
require_once('db.php');
$offset = 6;
$page_num = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = ($page_num-1)*$offset;

$total_records = countAll('users');
$number_of_pages = ceil($total_records/$offset);
///////////// SORT USERS ///////
$field = isset($_GET['field']) ? $_GET['field'] : 'name';
$order = isset($_GET['order']) ? $_GET['order'] : 'asc';
$newOrder = ($order=='asc')?'desc':'asc';

///////////// SELECT USERS /////
$allUsers = findAll('users',$where='', "LIMIT $limit,$offset"," ORDER BY `$field` $order");
////////////////////////////
//var_dump($allUsers);

?>
<link href="css/form.css" rel="stylesheet" type="text/css" />

<table width="80%" border="1" cellpadding="5" cellspacing="0">
  <thead>
    <tr>
      <th>#</th>
      <th><a href="<?=$_SERVER['PHP_SELF']?>?field=name&order=<?=$newOrder?>">Name</a></th>
      <th><a href="<?=$_SERVER['PHP_SELF']?>?field=email&order=<?=$newOrder?>">Email</a></th>
      <th>Status</th>
      <th>Added Date</th>
      <th>Modified Date</th>
    </tr>
  </thead>
  <tbody>
<?php
$i = 1;
while ($user = mysql_fetch_assoc($allUsers))
{
  $color = (($i++ % 2)==0)?'lightgreen' :'lightblue';
?>
    <tr style="background-color:<?=$color?>">
      <td><a href="edit_user.php?id=<?=$user['id']?>"><?=$user['id']?></a></td>
      <td><?=$user['name']?></td>
      <td><a href="mailto:<?=$user['email']?>"><?=$user['email']?></a></td>
      <td><?=$user['status']?></td>
      <td><?=date('d-M-Y h:i:s a l',strtotime($user['register_date']))?></td>
      <td><?=date('d-M-Y h:i:s a l',strtotime($user['modify_date']))?></td>
    </tr>
<?php
 //pr($user);
}
?>
  </tbody>
</table>

<table  border="1" cellpadding="0" cellspacing="0" width="80%" >

    <tr>
<?php for($i=1 ; $i<= $number_of_pages ; $i++) {
  if($i == $page_num){
?>
<td align="center" style="color:red;font-size:30px;"><?=$i?></td>
<?php
  } else {
  ?>
      <td align="center"><a href="show_all_users.php?field=name&order=<?=$order?>&page=<?=$i?>"><?=$i?></a></td>
<?php
  }//end else
}?>
    </tr>

</table>
